package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;

import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.bean.UserBean;
import top.hidoctor.auscultation.bean.YangbenBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.ExampleChooseContract;
import top.hidoctor.auscultation.net.c.LabelSdReq;
import top.hidoctor.auscultation.net.c.SampleSearchReq2;
import top.hidoctor.auscultation.net.req.UserReq;
import top.hidoctor.auscultation.net.req.AuditSdReq;
import top.hidoctor.auscultation.net.req.SampleSearchReq;

public class ExampleChoosePresenter extends PresenterWrapper<ExampleChooseContract.View> implements ExampleChooseContract.Presenter {


    public ExampleChoosePresenter(Context context, ExampleChooseContract.View view) {
        super(context, view);
    }

    @Override
    public void getYbList(final SampleSearchReq req, SwipeRefreshLayout refreshLayout) {
        if (refreshLayout != null && !refreshLayout.isRefreshing()) {
            mView.showLoading();
        }
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.getSampleList(maps)
                .compose(this.<YangbenBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<YangbenBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<YangbenBean> response) {
                        mView.hideLoading();
                        mView.getYbListSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }


    @Override
    public void getYbList2(SampleSearchReq2 req, SwipeRefreshLayout refreshLayout) {
        if (refreshLayout != null && !refreshLayout.isRefreshing()) {
            mView.showLoading();
        }
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.getDifficult(maps)
                .compose(this.<YangbenBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<YangbenBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<YangbenBean> response) {
                        mView.hideLoading();
                        mView.getYbListSuc2(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void getUserList(UserReq req, SwipeRefreshLayout refreshLayout) {
        if (refreshLayout != null && !refreshLayout.isRefreshing()) {
            mView.showLoading();
        }
        add(mService.getUser(new NetMapEncryptUtils(req).getMaps())
                .compose(this.<UserBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<UserBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<UserBean> response) {
                        mView.hideLoading();
                        mView.getUserListSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void addLabel(LabelSdReq req) {
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.addLabelSd(new PatchRequest(maps).getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.addLabelSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void addLabel2(AuditSdReq req) {
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.addAuditSd(new PatchRequest(maps).getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.addLabelSuc2();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }
}
